import requests
import RPi.GPIO as GPIO
import time

def control_light(state):
    """
    LED控制函数
    参数：
        state : int - 1开灯/0关灯
    功能：
        - 初始化GPIO设置
        - 根据输入控制GPIO3引脚电平
        - 自动处理GPIO资源清理
    """
    try:
        # 禁用GPIO警告（避免重复初始化报错）
        GPIO.setwarnings(False)
        # 设置GPIO模式为BCM编码
        GPIO.setmode(GPIO.BCM)
        # 初始化3号GPIO为输出模式
        GPIO.setup(3, GPIO.OUT)
        
        # 根据输入设置电平
        if state == 1:
            GPIO.output(3, GPIO.HIGH)
            while True:
                time.sleep(1)
        elif state == 0:
            GPIO.output(3, GPIO.LOW)
            while True:
                time.sleep(1)
        else:
            raise ValueError("无效状态值，只接受0或1")
            
    except Exception as e:
        print(f"操作失败：{str(e)}")
    finally:
        # 释放GPIO资源（实际使用时可注释掉保持状态）
        GPIO.cleanup()


# 获取用户输入的问题
user_input = input("请输入问题：")

url = "https://api.ap.siliconflow.com/v1/chat/completions"

payload = {
    "model": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
    "stream": False,
    "max_tokens": 512,
    "temperature": 0.7,
    "top_p": 0.7,
    "top_k": 50,
    "frequency_penalty": 0.5,
    "n": 1,
    "stop": [],
    "messages": [
        {
            "role": "system",
            "content": "请严格按以下规则判断：若用户希望房间更亮，回复1；否则回复0。只需输出数字。"
        },
        {
            "role": "user",
            "content": user_input
        }
    ]
}

headers = {
    "Authorization": "Bearer sk-hqzicwylthrvcltzzbwtsvgcjedbimykpiekkmolnyhibhyj",
    "Content-Type": "application/json"
}


# 使用示例
if __name__ == "__main__":
    try:
        response = requests.post(url, json=payload, headers=headers)
        response_data = response.json()
        answer = response_data['choices'][0]['message']['content'].strip()
        
        if answer == '1':
            control_light(1)
        elif answer == '0':
            control_light(0)
    except Exception as e:
        print("处理请求时发生错误:", e)